﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Common;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.DaoTao
{
    public partial class FormQuanLyBaoLuu_VaoHocLai : BaseDialog
    {
        bool allowUpdate = false;
        int entityToBeAdded = 0;
        HocVien hocVien;
        public FormQuanLyBaoLuu_VaoHocLai(string maHocVien)
        {
            InitializeComponent();
            hocVien = BizHocVien.GetByID(maHocVien);
            if (hocVien != null)
            {
                InitGridLop();
                LoadKhoaHoc();
            }

        }

        void InitGridLop()
        {
            gridDataLop.AutoGenerateColumns = false;
            colTenLop.DataPropertyName = "TenLopKhoaNganh";
            colTenCSDT.DataPropertyName = "TenCoSoDaoTao";
        }

        void LoadKhoaHoc()
        {
            allowUpdate = false;
            var ls = BizKhoaHoc.GetList();
            cmbKhoaHoc.DataSource = ls;
            cmbKhoaHoc.DisplayMember = "TenKhoaHoc";
            cmbKhoaHoc.ValueMember = "MaKhoaHocGUID";
            allowUpdate = true;
            LoadNganhHoc();
        }

        void LoadNganhHoc()
        {
            allowUpdate = false;
            var khoaHoc = cmbKhoaHoc.SelectedItem as KhoaHoc;
            if (khoaHoc != null)
            {
                var ls = BizKhoaHoc_NganhDaoTao.GetListByKhoaHoc2(khoaHoc.MaKhoaHocGUID);
                cmbNganhHoc.DataSource = ls;
                cmbNganhHoc.DisplayMember = "TenNganhDaoTao";
                cmbNganhHoc.ValueMember = "MaNganhDaoTaoGUID";
            }
            else
            {
                cmbNganhHoc.DataSource = null;

            }
            allowUpdate = true;
            LoadLop();
        }

        void LoadLop()
        {
            allowUpdate = false;
            var khoaHoc = cmbKhoaHoc.SelectedItem as KhoaHoc;
            var nganhHoc = cmbNganhHoc.SelectedItem as NganhDaoTao;
            if (khoaHoc != null && nganhHoc != null)
            {
                var ls = BizLopKhoaNganh.GetList(khoaHoc.MaKhoaHocGUID, nganhHoc.MaNganhDaoTaoGUID);
                bdLop.DataSource = ls;
                gridDataLop.DataSource = bdLop;
                if (ls != null && ls.Count > 0)
                    btnVaoHocLai.Enabled = true;
                else
                    btnVaoHocLai.Enabled = false;
            }
            else
            {
                bdLop.DataSource = null;
                btnVaoHocLai.Enabled = false;
            }
            allowUpdate = true;
        }

        private void bdLop_CurrentChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
            }
        }

        private void cmbKhoaHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                LoadNganhHoc();
            }
        }

        private void cmbNganhHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                LoadLop();
            }
        }

        private void FormQuanLyBaoLuu_BaoLuu_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (entityToBeAdded > 0)
                DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnVaoHocLai_Click(object sender, EventArgs e)
        {
            var current = bdLop.Current as LopKhoaNganh;
            if (current != null)
            {
                var dg = new FormQuanLyBaoLuu_VaoHocLai_ChiTiet(hocVien, current);
                if (dg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    DialogUtils.ShowMessage(string.Format("Đã chuyển học viên vào học lớp: {0} thành công.", current.TenLopKhoaNganh));
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                    DialogResult = System.Windows.Forms.DialogResult.No;
            }
        }
    }
}
